<HTML>
 
<!-- Mirrored from www.javapractices.com/apps/movies/javadoc/src-html/hirondelle/movies/util/Util.html by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:30:21 GMT -->
<HEAD>
  <TITLE>
Util.java
  </TITLE>
  <LINK REL ='stylesheet' TYPE='text/css' HREF='../../../../highlight.css' TITLE='Style'>
 </HEAD>
 <BODY>
<PRE>
<span class='keyword'>package</span> hirondelle.movies.util;<a name=line.1></a>
<a name=line.2></a>
<span class='keyword'>import</span> java.util.*;<a name=line.3></a>
<span class='keyword'>import</span> java.util.logging.Logger;<a name=line.4></a>
<span class='keyword'>import</span> java.math.BigDecimal;<a name=line.5></a>
<a name=line.6></a>
<span class='keyword'>import</span> hirondelle.movies.exception.InvalidInputException;<a name=line.7></a>
<a name=line.8></a>
<span class='keyword'>import</span> java.text.SimpleDateFormat;<a name=line.9></a>
<span class='keyword'>import</span> java.text.DateFormat;<a name=line.10></a>
<span class='keyword'>import</span> java.text.ParseException;<a name=line.11></a>
<a name=line.12></a>
<span class='comment'>/**<a name=line.13></a>
 Static convenience methods for common tasks, which eliminate code duplication.<a name=line.14></a>
*/</span><a name=line.15></a>
<span class='keyword'>public</span> <span class='keyword'>final</span> <span class='keyword'>class</span> Util {<a name=line.16></a>
<a name=line.17></a>
   <span class='comment'>/**<a name=line.18></a>
    Return &lt;tt&gt;true&lt;/tt&gt; only if &lt;tt&gt;aText&lt;/tt&gt; is not null,<a name=line.19></a>
    and is not empty after trimming. (Trimming removes both<a name=line.20></a>
    leading/trailing whitespace and ASCII control characters.)<a name=line.21></a>
   <a name=line.22></a>
    &lt;P&gt; For checking argument validity, {@link Args#checkForContent} should <a name=line.23></a>
    be used instead of this method.<a name=line.24></a>
    <a name=line.25></a>
    &lt;P&gt;This method is particularly useful, since it is very commonly required.<a name=line.26></a>
   <a name=line.27></a>
    @param aText possibly-null.<a name=line.28></a>
   */</span><a name=line.29></a>
   <span class='keyword'>public</span> <span class='keyword'>static</span> <span class='keyword'>boolean</span> textHasContent(String aText) {<a name=line.30></a>
     <span class='keyword'>return</span> (aText != <span class='keyword'>null</span>) &amp;&amp; (aText.trim().length() &gt; <span class='literal'>0</span>);<a name=line.31></a>
   }<a name=line.32></a>
<a name=line.33></a>
  <span class='comment'>/**<a name=line.34></a>
   Return &lt;tt&gt;true&lt;/tt&gt; only if &lt;tt&gt;aNumber&lt;/tt&gt; is in the range <a name=line.35></a>
   &lt;tt&gt;aLow..aHigh&lt;/tt&gt; (inclusive).<a name=line.36></a>
  <a name=line.37></a>
   &lt;P&gt; For checking argument validity, {@link Args#checkForRange} should <a name=line.38></a>
   be used instead of this method.<a name=line.39></a>
  <a name=line.40></a>
   @param aLow less than or equal to &lt;tt&gt;aHigh&lt;/tt&gt;.<a name=line.41></a>
  */</span><a name=line.42></a>
  <span class='keyword'>static</span> <span class='keyword'>public</span> <span class='keyword'>boolean</span> isInRange( <span class='keyword'>int</span> aNumber, <span class='keyword'>int</span> aLow, <span class='keyword'>int</span> aHigh ){<a name=line.43></a>
    <span class='keyword'>if</span> (aLow &gt; aHigh) {<a name=line.44></a>
      <span class='keyword'>throw</span> <span class='keyword'>new</span> IllegalArgumentException(<span class='literal'>"Low is greater than High."</span>);<a name=line.45></a>
    }<a name=line.46></a>
    <span class='keyword'>return</span> (aLow <= aNumber &amp;&amp; aNumber <= aHigh);<a name=line.47></a>
  }<a name=line.48></a>
<a name=line.49></a>
  <span class='comment'>/**<a name=line.50></a>
   Return &lt;tt&gt;true&lt;/tt&gt; if &lt;tt&gt;aBoolean&lt;/tt&gt; equals "true" (ignore case), or <a name=line.51></a>
   &lt;tt&gt;false&lt;/tt&gt; if &lt;tt&gt;aBoolean&lt;/tt&gt; equals "false" (ignore case).<a name=line.52></a>
  <a name=line.53></a>
  &lt;P&gt;Note that this behavior is different from that of &lt;tt&gt;Boolean.getValue&lt;/tt&gt;.<a name=line.54></a>
  <a name=line.55></a>
   @param aBoolean equals "true" or "false" (not case-sensitive).<a name=line.56></a>
  */</span><a name=line.57></a>
  <span class='keyword'>public</span> <span class='keyword'>static</span> Boolean parseBoolean(String aBoolean){<a name=line.58></a>
    <span class='keyword'>if</span> ( aBoolean.equalsIgnoreCase(<span class='literal'>"true"</span>) ) {<a name=line.59></a>
      <span class='keyword'>return</span> Boolean.TRUE;<a name=line.60></a>
    }<a name=line.61></a>
    <span class='keyword'>else</span> <span class='keyword'>if</span> ( aBoolean.equalsIgnoreCase(<span class='literal'>"false"</span>) ) {<a name=line.62></a>
      <span class='keyword'>return</span> Boolean.FALSE;<a name=line.63></a>
    }<a name=line.64></a>
    <span class='keyword'>else</span> {<a name=line.65></a>
      <span class='keyword'>throw</span> <span class='keyword'>new</span> IllegalArgumentException(<span class='literal'>"Cannot parse into Boolean: "</span> + aBoolean);<a name=line.66></a>
    }<a name=line.67></a>
  }<a name=line.68></a>
  <a name=line.69></a>
  <span class='comment'>/**<a name=line.70></a>
   Return a {@link Logger} whose name follows a specific naming convention.<a name=line.71></a>
  <a name=line.72></a>
   &lt;P&gt;The conventional logger names are taken as   <a name=line.73></a>
   &lt;tt&gt;aClass.getPackage().getName()&lt;/tt&gt;.<a name=line.74></a>
   <a name=line.75></a>
   &lt;P&gt;Logger names appearing in the &lt;tt&gt;logging.properties&lt;/tt&gt; config file<a name=line.76></a>
   must match the names returned by this method.<a name=line.77></a>
  */</span><a name=line.78></a>
  <span class='keyword'>public</span> <span class='keyword'>static</span> Logger getLogger(Class&lt;?&gt; aClass){<a name=line.79></a>
     <span class='keyword'>return</span> Logger.getLogger(aClass.getPackage().getName());  <a name=line.80></a>
   }<a name=line.81></a>
  <a name=line.82></a>
  <span class='comment'>/**<a name=line.83></a>
  Parse text into a {@link Date}. If the text has no content, then return &lt;tt&gt;null&lt;/tt&gt;.<a name=line.84></a>
  */</span><a name=line.85></a>
  <span class='keyword'>public</span> <span class='keyword'>static</span> Date parseDate(String aDate, String aName) <span class='keyword'>throws</span> InvalidInputException {<a name=line.86></a>
    Date result = <span class='keyword'>null</span>;<a name=line.87></a>
    <span class='keyword'>if</span>( textHasContent(aDate) ) {<a name=line.88></a>
      DateFormat FORMAT = <span class='keyword'>new</span> SimpleDateFormat(DATE_FORMAT);<a name=line.89></a>
      FORMAT.setLenient(<span class='keyword'>false</span>);<a name=line.90></a>
      <span class='keyword'>try</span> {<a name=line.91></a>
        result = FORMAT.parse(aDate);<a name=line.92></a>
      }<a name=line.93></a>
      <span class='keyword'>catch</span> (ParseException ex){<a name=line.94></a>
        throwEx(aName + <span class='literal'>" is not a valid date: "</span> + aDate);<a name=line.95></a>
      }<a name=line.96></a>
    }<a name=line.97></a>
    <span class='keyword'>return</span> result;<a name=line.98></a>
  }<a name=line.99></a>
  <a name=line.100></a>
  <span class='comment'>/**<a name=line.101></a>
   Format an arbitrary Object, into a form suitable for the end user.<a name=line.102></a>
  */</span><a name=line.103></a>
  <span class='keyword'>public</span> <span class='keyword'>static</span> String format(Object aObject){<a name=line.104></a>
    String result = <span class='literal'>""</span>;<a name=line.105></a>
    <span class='keyword'>if</span>( aObject != <span class='keyword'>null</span> ){<a name=line.106></a>
      <span class='keyword'>if</span>( aObject <span class='keyword'>instanceof</span> Date ){<a name=line.107></a>
        Date date = (Date)aObject;<a name=line.108></a>
        DateFormat FORMAT = <span class='keyword'>new</span> SimpleDateFormat(DATE_FORMAT);<a name=line.109></a>
        FORMAT.setLenient(<span class='keyword'>false</span>);<a name=line.110></a>
        result = FORMAT.format(date);<a name=line.111></a>
      }<a name=line.112></a>
      <span class='keyword'>else</span> {<a name=line.113></a>
        result = String.valueOf(aObject);<a name=line.114></a>
      }<a name=line.115></a>
    }<a name=line.116></a>
    <span class='keyword'>return</span> result;<a name=line.117></a>
  }<a name=line.118></a>
  <a name=line.119></a>
  <a name=line.120></a>
  <span class='comment'>/**<a name=line.121></a>
   Parse text into a {@link BigDecimal}. If the text has no content, then return &lt;tt&gt;null&lt;/tt&gt;.<a name=line.122></a>
 */</span><a name=line.123></a>
  <span class='keyword'>public</span> <span class='keyword'>static</span> BigDecimal parseBigDecimal(String aBigDecimal, String aName) <span class='keyword'>throws</span> InvalidInputException {<a name=line.124></a>
    BigDecimal result = <span class='keyword'>null</span>;<a name=line.125></a>
    <span class='keyword'>if</span> ( textHasContent(aBigDecimal) ) {<a name=line.126></a>
      <span class='keyword'>try</span> {<a name=line.127></a>
        result = <span class='keyword'>new</span> BigDecimal(aBigDecimal);<a name=line.128></a>
      }<a name=line.129></a>
      <span class='keyword'>catch</span> (NumberFormatException exception){<a name=line.130></a>
        throwEx(aName + <span class='literal'>" is not a valid number."</span>);<a name=line.131></a>
      }<a name=line.132></a>
    }<a name=line.133></a>
    <span class='keyword'>return</span> result;<a name=line.134></a>
  }<a name=line.135></a>
<a name=line.136></a>
  <span class='comment'>// PRIVATE //<a name=line.137></a>
</span>  <a name=line.138></a>
  <span class='comment'>/** The application's date format. */</span><a name=line.139></a>
  <span class='keyword'>private</span> <span class='keyword'>static</span> <span class='keyword'>final</span> String DATE_FORMAT = <span class='literal'>"yyyy-MM-dd"</span>;<a name=line.140></a>
  <a name=line.141></a>
  <span class='keyword'>private</span> <span class='keyword'>static</span> <span class='keyword'>void</span> throwEx(String aMessage) <span class='keyword'>throws</span> InvalidInputException {<a name=line.142></a>
    InvalidInputException ex = <span class='keyword'>new</span> InvalidInputException();<a name=line.143></a>
    ex.add(aMessage);<a name=line.144></a>
    <span class='keyword'>throw</span> ex;<a name=line.145></a>
  }<a name=line.146></a>
}<a name=line.147></a>
 <a name=line.148></a>
</PRE><a name=line.149></a>
 </BODY><a name=line.150></a>
<HTML><a name=line.151></a>
